package fr.jade.fraclite;

import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.Interface;
import org.objectweb.fractal.api.Type;

import fr.jade.fraclite.api.ComponentInterface;


/**
 * Provides a basic implementation of the {@link ComponentInterface} interface.
 */
public class BasicComponentInterface implements ComponentInterface {
  
  /**
   * The impl link
   */
  Object impl;
  
  /**
   * The component to which this component interface belongs.
   */
  protected Component owner;

  /**
   * The name of this component interface.
   */
  protected String name;

  /**
   * The type of this component interface.
   */
  protected Type type;

  /**
   * The flags of this component interfaces. These flags indicates if this
   * component interface is an internal interface or not, and if it has an
   * permanently associated interceptor or not.
   */
  protected boolean isInternal;

  // -------------------------------------------------------------------------
  // Constructors
  // -------------------------------------------------------------------------

  /**
   * Constructs a new {@link BasicComponentInterface}.
   *
   * @param owner the component to which the interface belongs. If this
   *      parameter is <tt>null</tt> the owner is set to the interface itself,
   *      which must therefore be a {@link Component} interface.
   * @param name the name of the interface.
   * @param type the type of the interface.
   * @param isInternal <tt>true</tt> if the interface is an internal interface.
   * @param impl the object that implements the interface.
   */

  public BasicComponentInterface (Component owner, String name, Type type, boolean isInternal, Object impl) {
    this.owner = owner;
    this.name = name;
    this.type = type;
    this.isInternal = isInternal;
    this.setFcItfImpl(impl);
  }

  // -------------------------------------------------------------------------
  // Implementation of the Interface interface
  // -------------------------------------------------------------------------

  public Component getFcItfOwner () {
    return owner;
  }

  public String getFcItfName () {
    return name;
  }

  public Type getFcItfType () {
    return type;
  }

  public boolean isFcInternalItf () {
    return isInternal;
  }
  
  // -------------------------------------------------------------------------
  // Implementation of the ComponentInterface interface
  // -------------------------------------------------------------------------

  public Object getFcItfImpl() {
    return impl;
  }

  public void setFcItfImpl(Object impl) {
    this.impl = impl;
  }
  
  public void setFcItfName (final String name) {
    this.name = name;
  }

  // -------------------------------------------------------------------------
  // Overriden Object methods
  // -------------------------------------------------------------------------

  /**
   * Tests if the given object is equal to this component interface.
   *
   * @param o the object to be compared to this component interface.
   * @return <tt>true</tt> if o is a component interface with the same name and
   *      owner as this component interface, and if both are internal (resp.
   *      external) component interfaces.
   */

  public boolean equals (final Object o) {
    if (o == this) {
      return true;
    }
    if (o instanceof Interface) {
      Interface itf = (Interface)o;
      return
        getFcItfOwner() == itf.getFcItfOwner() && // TODO ou equals?
        getFcItfName().equals(itf.getFcItfName()) &&
        isFcInternalItf() == itf.isFcInternalItf();
    }
    return false;
  }

}
